﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Studio.Network.Utilities
{

    [ComVisible(true)]
    public partial class MshtmlControl : UserControl, IOleClientSite
    {

        public MshtmlControl()
        {
            InitializeComponent();
        }


        public string FetchUserAgent()
        {
            mshtml.IHTMLDocument2 doc = new mshtml.HTMLDocumentClass();
            return doc.parentWindow.clientInformation.userAgent;

        }


        [DispId(-5512)]
        public virtual int IDispatch_Invoke_Handler()
        {
            System.Diagnostics.Debug.WriteLine("-5512");
            return (int)(BrowserOptions.DownloadOnly | BrowserOptions.DontRunActiveX | BrowserOptions.NoJava | BrowserOptions.NoScripts | BrowserOptions.NoActiveXDownload);
        }


        public mshtml.IHTMLDocument2 FetchUrl(string Url)
        {
            if (true == string.IsNullOrEmpty(Url))
                return null;

            mshtml.IHTMLDocument4 doc = new mshtml.HTMLDocumentClass();
            mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)doc;

            IOleObject obj = (IOleObject)doc2;
            obj.SetClientSite(this);

            doc2.write("<html></html>");
            doc2.close();

            doc2 = doc.createDocumentFromUrl(Url, "");

            while (doc2.readyState != "complete")
            {
                System.Diagnostics.Debug.WriteLine(doc2.readyState);
                System.Windows.Forms.Application.DoEvents();
            }

            System.Diagnostics.Debug.WriteLine(doc2.body.innerHTML);

            return doc2;
        }


        #region IOleClientSite Members

        public int SaveObject()
        {
            return 0;
        }

        public int GetMoniker(int dwAssign, int dwWhichMoniker, out object moniker)
        {
            moniker = this;
            return 0;
        }

        public int GetContainer(out object container)
        {
            container = this;
            return 0;
        }

        public int ShowObject()
        {
            return 0;
        }

        public int OnShowWindow(int fShow)
        {
            return 0;
        }

        public int RequestNewObjectLayout()
        {
            return 0;
        }

        #endregion



    }
}
